package test.designmode.buttonlistener2;

import java.lang.reflect.Method;

/**
 * @brief  事件包含: 执行者, 执行的方法, 执行的参数;
 *                  该类主要是对反射的处理做了封装, 仅此而已; 将反射封装为事件;
 *
 * @author linshuo
 *
 */
@SuppressWarnings("all")
public class Job {
    // 要执行方法的对象
    private Object object;
    // 要执行的方法名称
    private String methodName;
    // 要执行方法的参数
    private Object[] params;
    // 要执行方法的参数类型
    private Class[] paramTypes;

    public Job() {
    }

    public Job(Object object, String methodName, Object... args) {
        this.object = object;
        this.methodName = methodName;
        this.params = args;
        contractParamTypes(this.params);
    }

    // 根据参数数组生成参数类型数组
    private void contractParamTypes(Object[] params) {
        this.paramTypes = new Class[params.length];
        for (int i = 0; i < params.length; i++) {
            this.paramTypes[i] = params[i].getClass();
        }
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Object getObject() {
        return object;
    }
    
    public Object[] getParams() {
        return params;
    }

    public void setParams(Object[] params) {
        this.params = params;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public Class[] getParamTypes() {
        return paramTypes;
    }
    // 若干setter getter省略
    public void setParamTypes(Class[] paramTypes) {
        this.paramTypes = paramTypes;
    }

    // 执行该 对象的该方法
    public void invoke() throws Exception {
        Method method = object.getClass().getMethod(this.getMethodName(), this.getParamTypes());
        if (null == method) {
            return;
        }
        method.invoke(this.getObject(), this.getParams());
    }
}
